package com.spyx.acl.controller;

import com.atyx.spyx.model.acl.Role;
import com.atyx.spyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spyx.acl.service.RoleService;
import com.spyx.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/acl/role")
@Api(tags = "角色管理")
//@CrossOrigin
public class RoleController {

    @Autowired
    private RoleService roleService;

    //查询角色列表(分页+模糊查)
    @ApiOperation("获取角色列表和模糊查")
    @GetMapping("/{page}/{limit}")
    public Result getList(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable("page")Integer page,
            @ApiParam(name = "limit",value = "每页显示条数",required = true)
            @PathVariable("limit") Integer limit,
            @ApiParam(name = "roleQueryVo",value = "角色查询对象",required = false)
            RoleQueryVo roleQueryVo   //用来接受模糊查条件值
            ){
        //1.创建分页工具类对象
        Page p1 = new Page(page,limit);
        //2.调取service层代码完成分页和模糊查询
        IPage<Role> roleIPage= roleService.selectRoleList(p1,roleQueryVo);

        return Result.ok(roleIPage);
    }

    /*请求发送参数的方式:
       1.路径传参 restful风格     /{page}/{limit}    接收数据: @PathVariable("page")
       2.地址拼接      xxxx?key=value&key2=value2   接收数据: (1) 方法的参数名=key (2) @RequestParam
          login?username=xxx&password=xx           login(String username,String password)
       3.json格式   {roleName: xxxx}               @RequestBody

     */
    //保存角色
    @PostMapping("/save")
    @ApiOperation("新增角色")
    public Result save(@RequestBody Role role){
        roleService.save(role);
        return Result.ok();
    }

    //主键查询
    @GetMapping("/get/{id}")
    @ApiOperation("主键查询")
    public Result findById(@PathVariable("id") Integer rid){
        Role role = roleService.getById(rid);
        return Result.ok(role);
    }

    @PutMapping("update")
    @ApiOperation("修改角色")
    public Result update(@RequestBody Role role){
        roleService.updateById(role);
        return Result.ok();
    }

    @DeleteMapping("remove/{id}")
    @ApiOperation("删除某个角色")
    public Result deleteById(@PathVariable("id")Integer rid){
        roleService.removeById(rid);
        return Result.ok();
    }

    @DeleteMapping("batchRemove")
    @ApiOperation("批量删除角色")
    public Result deleteByIds(@RequestBody List<Integer> list){
        roleService.removeByIds(list);
        return Result.ok();
    }

}
